package br.com.controleFrota;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class RelatorioLog extends Activity {

	
	public BaseDados dh;
	private TextView output;
	StringBuilder sb;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.relatorio);

		this.dh = new BaseDados(this);
		
		List<String> names = this.dh.selectAllLog();
		
		sb = new StringBuilder();
        sb.append("Base:\n");
        
		for (String name : names) {
			Log.d("TAG", "Base:"+name);
			sb.append(name + "\r\n");
		}
		
//		this.gravarArquivo();
		
		this.output = (TextView) this.findViewById(R.id.textView1);
		
		this.output.setText(sb.toString());
		
		
	}
	
	public void gravarArquivo()
	{
		String lstrNomeArq;
        File arq;
        byte[] dados;
        try
        {

            lstrNomeArq = "Log_Evento.txt";
             
            arq = new File(Environment.getExternalStorageDirectory(), lstrNomeArq);
            FileOutputStream fos;
             
            //transforma o texto digitado em array de bytes
            dados = sb.toString().getBytes();
             
            fos = new FileOutputStream(arq);
             
            //escreve os dados e fecha o arquivo
            fos.write(dados);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            trace("Erro : " + e.getMessage());
        }    
	}
	
	public void toast (String msg)
    {
        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
    } 
     
	
	private void trace (String msg) 
    {
        toast (msg);
    }

}